// logs.js
let timer
let app = getApp();
Page({
  data: {
    winHeight: "", //窗口高度
    show:true,//答题卡是否显示

    currentTab: 0, //预设默认选中的题目索引
    num: 1, //当前习题展示

    list: [{
        id: 1,
        name: "请把学习重点向大家提示一下",
        type: '单选题',
        option: [{
          answer: 'A',
          title: '选项1',
          check: false
        }, {
          answer: 'B',
          title: '选项2',
          check: false
        }, {
          answer: 'C',
          title: '选项3',
          check: false
        }, {
          answer: 'D',
          title: '选项4',
          check: false
        }]
      },
      {
        id: 2,
        name: "习题二",
        type: '单选题',
        option: [{
          answer: 'A',
          title: '选项1',
          check: false
        }, {
          answer: 'B',
          title: '选项2',
          check: false
        }]
      },
      {
        id: 3,
        name: "习题三",
        type: '多选题',
        option: [{
          answer: 'A',
          title: '选项1',
          check: false
        }, {
          answer: 'B',
          title: '选项2',
          check: false
        }, {
          answer: 'C',
          title: '选项3',
          check: false
        }, {
          answer: 'D',
          title: '选项4',
          check: false
        }]
      },
      {
        id: 4,
        name: "习题四",
        type: '多选题',
        option: [{
          answer: 'A',
          title: '选项1',
          check: false
        }, {
          answer: 'B',
          title: '选项2',
          check: false
        }, {
          answer: 'C',
          title: '选项3',
          check: false
        }]
      }
    ],
    checkedList: [],
    date: '00:00', //考试倒计时分钟
  },

  // 滚动切换标签样式
  switchTab: function (e) {
    let tips = this.data.list.length - 1
    if (e.detail.current == tips) {
      wx.showToast({
        title: '已经是最后一题了',
        icon: 'none'
      })
    }

    this.setData({
      currentTab: e.detail.current,
      num: parseInt(e.detail.current) + 1
    });

  },

  // 选择
  choose(e) {
    // console.log(e.target.dataset.num1)
    // console.log(e.target.dataset.num2)
    // console.log(e.target.dataset.type)
    let index1 = e.target.dataset.num1
    let index2 = e.target.dataset.num2
    let type = e.target.dataset.type

    let list = this.data.list
    let checkedList = this.data.checkedList

    // 先判断是否是单选
    if (type == '单选题') {
      list[index1].option.forEach(item => {
        item.check = false
      });
      list[index1].option[index2].check = true //修改当前题目状态
      checkedList[index1].choose = list[index1].option[index2].answer //当前题目单选选中赋值
    } else if (type == '多选题') { //判断多选
      list[index1].option[index2].check = !list[index1].option[index2].check //修改当前题目状态
      let datas = []
      list[index1].option.forEach(item2 => { //当前题目多选选中
        if (item2.check == true) {
          datas.push(item2.answer)
        }
      });
      checkedList[index1].choose = datas.toString() //当前题目多选选中赋值
    }
    console.log(checkedList)
    // console.log(list)

    this.setData({
      list: list,
      checkedList: checkedList
    })
  },

  // 提交
  submit() {
    var that = this
    // console.log(this.data.checkedList)
    for(let i=0;i<this.data.checkedList.length;i++){
      console.log(this.data.checkedList[i].choose)
      if(!this.data.checkedList[i].choose || this.data.checkedList[i].choose==''){
        wx.showModal({
          title: '交卷',
          content: '您还有题目未完成，确定交卷吗？',
          success: function (res) {
            if (res.confirm) {
              console.log('确定')
              that.handOver()
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
        return
      }
    }
    wx.showModal({
      title: '交卷',
      content: '答题完成，确定交卷吗？',
      success: function (res) {
        if (res.confirm) {
          console.log('确定')
          that.handOver()
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },

  // 交卷
  handOver(){
    console.log(this.data.checkedList)
  },

  // 答题卡
  card(){
    let show = this.data.show
    this.setData({
      show:!show
    })
  },

  onLoad: function () {
    var that = this;
    //  高度自适应
    wx.getSystemInfo({
      success: function (res) {
        var clientHeight = res.windowHeight,
          clientWidth = res.windowWidth,
          rpxR = 750 / clientWidth;
        var calc = clientHeight * rpxR - 200;
        that.setData({
          winHeight: calc
        });
      }
    });

    let checkedList = []
    this.data.list.forEach(item => {
      let str = {
        id: item.id,
        choose: ''
      }
      checkedList.push(str)
    });
    // console.log(checkedList)
    this.setData({
      checkedList: checkedList
    })

    //定时任务开启
    this.countTime()
  },
  // 倒计时
  countTime() {
    var that = this
    var leftTime = 45*60 // 45分钟转化成秒
    console.log(leftTime)
    timer = setInterval(() => { //获取当前时间
      leftTime = leftTime - 1
      let h = Math.floor(leftTime / 60 / 60 % 24); //时
      let m = Math.floor(leftTime / 60 % 60); //分
      let s = Math.floor(leftTime % 60); //秒
      if (s < 10) {
        s = "0" + s;
      }
      if (m < 10) {
        m = "0" + m;
      }
      if (h < 10) {
        h = "0" + h;
      }
      let time = h + ':' + m + ':' + s
      this.setData({
        date: time
      })
      if (time == "00:00:00") {
        // console.log('时间到了，提交！')

        clearInterval(timer);//清除定时器

        that.handOver(); //自动提交试卷

        wx.showModal({
          title: '提示',
          showCancel: false,
          content: '考试时间已到，自动提交试卷',
          success: function (res) {
            if (res.confirm) {
              wx.navigateBack({
                delta: 1,
              })
            }
          }
        })
        return
      }
    }, 1000);
  },
  //停止定时器
  onHide: function () {
    clearInterval(timer);
  },
  //停止定时器
  onUnload: function () {
    clearInterval(timer);
  }
})